package com.sundae.smarthomedemo.Activitys;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.kyleduo.switchbutton.SwitchButton;
import com.sundae.smarthomedemo.Beans.DeviceSQLBean;
import com.sundae.smarthomedemo.R;
import com.sundae.smarthomedemo.Utils.DaoControl;
import com.sundae.smarthomedemo.Utils.Utils;
import com.sundae.smarthomedemo.Utils.LogUtil;
import com.sundae.smarthomedemo.Utils.ToastUtils;
import com.xw.repo.BubbleSeekBar;

import immortalz.me.library.TransitionsHeleper;
import immortalz.me.library.bean.InfoBean;
import immortalz.me.library.method.ColorShowMethod;

public class ControlActivity extends BaseActivity implements View.OnClickListener {

    private ImageView img;
    private Button normal_btn,fast_button,energy_btn , eoc_button;
    private SwitchButton switchButton;
    private BubbleSeekBar bubbleSeekBar;
    private TextView title_tv , temp_tv, status_tv, temp2_tv , comment_tv;
    private ImageButton back_btn , setting_btn;

    private DeviceSQLBean deviceSQLBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);

        TransitionsHeleper.getInstance()
                .setShowMethod(new ColorShowMethod(R.color.grayColor,R.color.styleColor) {
                    @Override
                    public void loadCopyView(InfoBean bean, ImageView copyView) {
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(
                                ObjectAnimator.ofFloat(copyView, "scaleX", 1, 0),
                                ObjectAnimator.ofFloat(copyView, "scaleY", 1, 0)
                        );
                        set.setInterpolator(new AccelerateInterpolator());
                        set.setDuration(duration / 4 * 5).start();
                    }

                    @Override
                    public void loadTargetView(InfoBean bean, ImageView targetView) {
                    }
                })
                .show(this,null);

        if(selectDevice == null)
        {
            ToastUtils.showShortToast(getApplicationContext() , "出现错误");
            finish();
        }
        deviceSQLBean = selectDevice;

    }

    @Override
    protected void onStart() {
        super.onStart();
        initView();
    }

    private void initView()
    {
        img = (ImageView) findViewById(R.id.ctl_img);
        normal_btn = (Button) findViewById(R.id.ctl_model_nomal);
        fast_button = (Button) findViewById(R.id.ctl_model_fast);
        energy_btn = (Button) findViewById(R.id.ctl_model_energy);
        eoc_button = (Button) findViewById(R.id.ctl_model_eoc);
        setting_btn = (ImageButton) findViewById(R.id.ctl_setting);

        switchButton = (SwitchButton) findViewById(R.id.ctl_switch);

        bubbleSeekBar = (BubbleSeekBar) findViewById(R.id.ctl_seekBar);

        title_tv = (TextView) findViewById(R.id.ctl_title_name);
        temp_tv = (TextView) findViewById(R.id.ctl_temp_textview);
        status_tv = (TextView) findViewById(R.id.ctl_status_textview);
        temp2_tv = (TextView) findViewById(R.id.ctl_temp2_textview);
        comment_tv = (TextView) findViewById(R.id.ctl_comment_textview);

        back_btn = (ImageButton) findViewById(R.id.ctl_back);

        normal_btn.setOnClickListener(this);
        fast_button.setOnClickListener(this);
        energy_btn.setOnClickListener(this);
        eoc_button.setOnClickListener(this);
        setting_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ControlActivity.this , DeviceInfoActivity.class);
                intent.putExtra("isAdd" , false);
                startActivity(intent);
            }
        });

        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ControlActivity.this.finish();
            }
        });


        title_tv.setText(deviceSQLBean.getDevice_name());
        img.setImageResource(Utils.getImgRes(deviceSQLBean.getDevice_type()));
        status_tv.setText(deviceSQLBean.getRun_status());
        temp_tv.setText(deviceSQLBean.getTemp() + "℃");
        temp2_tv.setText("温度设定为 " + deviceSQLBean.getSetTemp() + "℃");
        switchButton.setChecked(deviceSQLBean.getPower());
        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                selectDevice.setPower(isChecked);
                if(isChecked)selectDevice.setRun_status("运行中");
                else selectDevice.setRun_status("停止运行");
            }
        });

        switch(deviceSQLBean.getModel_type())
        {
            case 0:
                normal_btn.performClick();
                break;
            case 1:
                fast_button.performClick();
                break;
            case 2:
                energy_btn.performClick();
                break;
            case 3:
                eoc_button.performClick();
                break;
        }

        bubbleSeekBar.setProgress(deviceSQLBean.getSetTemp());
        bubbleSeekBar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(int progress, float progressFloat) {
                temp2_tv.setText("温度设定为 " + progress + "℃");
            }

            @Override
            public void getProgressOnActionUp(int progress, float progressFloat) {
                LogUtil.e("getProgressOnActionUp" + progressFloat);
                selectDevice.setSetTemp(progress);
            }

            @Override
            public void getProgressOnFinally(int progress, float progressFloat) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        normal_btn.setBackground(ContextCompat.getDrawable(getApplicationContext() , R.drawable.model_select_button_nromal));
        fast_button.setBackground(ContextCompat.getDrawable(getApplicationContext() , R.drawable.model_select_button_nromal));
        energy_btn.setBackground(ContextCompat.getDrawable(getApplicationContext() , R.drawable.model_select_button_nromal));
        eoc_button.setBackground(ContextCompat.getDrawable(getApplicationContext() , R.drawable.model_select_button_nromal));

        normal_btn.setTextColor(Color.parseColor("#404040"));
        fast_button.setTextColor(Color.parseColor("#404040"));
        energy_btn.setTextColor(Color.parseColor("#404040"));
        eoc_button.setTextColor(Color.parseColor("#404040"));

        ((TextView)v).setBackground(ContextCompat.getDrawable(getApplicationContext() , R.drawable.model_select_button_pressed));
        ((TextView)v).setTextColor(Color.parseColor("#11d4b4"));

        switch(v.getId())
        {
            case R.id.ctl_model_nomal:
                selectDevice.setModel_type(0);
                break;
            case R.id.ctl_model_fast:
                selectDevice.setModel_type(1);
                break;
            case R.id.ctl_model_energy:
                selectDevice.setModel_type(2);
                break;
            case R.id.ctl_model_eoc:
                selectDevice.setModel_type(3);
                break;
        }
    }

    @Override
    protected void onStop() {
        DaoControl.getInstance(getApplicationContext()).update(selectDevice);
        super.onStop();
    }
}
